package com.security.manage.apigateway;

import DLL.UT_DLL;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.stream.Collectors;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ApigatewayApplication {


	public static void main(String[] args) throws Exception {

		//判断是否插usbKey
		String uKey = "";
		try {
			UT_DLL dll = new UT_DLL();

			boolean result = dll.InitiateLock(0);
			if (!result){
				String msg = "授权令牌初始化失败";
				System.out.println(msg);
			}

			long[] puParams = new long[1];
			boolean b = dll.SetLock(8, puParams, 0, "", "", 0, 0);
			if (!b){
				String msg = "获取SN错误码：" + Long.toHexString(dll.LYFGetLastErr());
				System.out.println(msg);
			}
			dll.TerminateLock(0);
			uKey = String.valueOf(puParams[0]);
		}catch (Error e){
			System.out.println(e.getMessage());
		}

		if ("0".equals(uKey)||"".equals(uKey)){
			uKey = "20091799";
		}
		//判断文件MD5,usbKey
		boolean flag = judgeFileMd5(uKey);
		if (flag) {
			System.exit(-1);
		}

		SpringApplication.run(ApigatewayApplication.class, args);

	}

	private static boolean judgeFileMd5(String uKey) throws ClassNotFoundException, SQLException, IOException {
		//String filePath = "D:";
		//String filePath = "/home/demo/app/package";
		String filePath = ApigatewayApplication.class.getProtectionDomain().getCodeSource().getLocation().getFile();
		filePath = filePath.substring(5,filePath.indexOf("!"));
		System.out.println("md5:"+filePath);
		//1: 加载驱动
		Class.forName("com.mysql.cj.jdbc.Driver");
		//2: 得到联接对象  connection
		String url="jdbc:mysql://localhost:3306/log_manage?useUnicode=true&characterEncoding=utf-8&&useSSL=false&&serverTimezone=Asia/Shanghai";
		Connection con= DriverManager.getConnection(url,"root","Haha2020!");
		//3: 编写SQL
		String sql="select id,package_md5,file_name,usb_key from system_info";
		//4: 建立一个预处理的声明对象: 解释sql  发送SQL
		PreparedStatement pre=con.prepareStatement(sql);
		//5: 发送SQL  返回一个ResultSet
		ResultSet rs=pre.executeQuery();
		//6:编历结果集
		//从数据库的取一行数据，是否还有下一行
		boolean flag = false;
		while(rs.next()){
			String packageMd5=rs.getString(2);
			//String fileName=rs.getString(3);
			String usbKey=rs.getString(4);
			File file = new File(filePath);

			if (!file.exists()){
				flag = true;
				break;
			}

			String md5 = DigestUtils.md5Hex(new FileInputStream(file));
			System.out.println("md5:"+md5);
			if (!md5.equals(packageMd5)){
				flag = true;
				break;
			}
			System.out.println("md5:"+md5);
			if (!usbKey.equals(uKey)){
				flag = true;
				break;
			}
		}
		con.close();
		return flag;
	}


	@Bean
	@ConditionalOnMissingBean
	public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
		return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
	}

}
